﻿using System;
using System.Collections.Generic;
using System.Web.Http;

using aier.business.logic;
using aier.dataaccess.model;

using log4net;

namespace aier.wx.api.selfhost.Controllers
{
    /// <summary>
    /// 筛查小组
    /// </summary>
    [RoutePrefix("api/ScreenTeam")]
    public class ScreenTeamController : ApiController
    {
        /// <summary>
        /// 日志
        /// </summary>
        public ILog log { get; set; }
        /// <summary>
        /// 筛查组逻辑
        /// </summary>
        public ScreenTeamBll screenTeamBll { get; set; }


        /// <summary>
        /// 获取所有筛查小组
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetAll")]
        public IHttpActionResult GetScreenAllTeams()
        {
            IEnumerable<ScreenTeamView> teamViews = screenTeamBll.GetScreenAllTeams();

            return Ok<IEnumerable<ScreenTeamView>>(teamViews);
        }
        /// <summary>
        /// 根据负责人ID查询筛查小组
        /// </summary>
        /// <param name="ownerId"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetScreenTeamByOwnerId")]
        public IHttpActionResult Get(string ownerId)
        {

            try
            {
              var teams =  screenTeamBll.GetScreenTeams(new Guid(ownerId));
                return Ok(teams);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }

        /// <summary>
        /// 根据筛查负责人域账号查询当前筛查小组
        /// 原程序使用：selectTeamIdByOwerName
        /// </summary>
        /// <param name="ownername">筛查负责人域账号（AIERCHINA\Lancy）</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetScreenTeamByDomainName")]
        public IHttpActionResult GetScreenTeamByOwnerName(string ownername)
        {
            try
            {
                ScreenTeamView screenTeam = screenTeamBll.QueryScreenTeamByOwnerName(ownername);
                return Ok<ScreenTeamView>(screenTeam);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }

        /// <summary>
        /// 根据当前登录用户Id获取筛查小组
        /// </summary>
        /// <param name="ownerid">用户Id</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetFirstScreenTeam")]
        public IHttpActionResult GetFirstScreenTeam(Guid ownerid)
        {
            try
            {
                ScreenTeamView screenTeam = screenTeamBll.QueryScreenTeamByOwnerId(ownerid);
                return Ok<ScreenTeamView>(screenTeam);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }

        /// <summary>
        /// 根据筛查小组名称查询当前筛查小组
        /// 原程序使用：getScreenteam
        /// </summary>
        /// <param name="name">筛查小组名称</param>
        /// <param name="hospital">医院ID</param>
        /// <param name="ownerid">负责人ID</param>
        /// <param name="statecode">状态０：可用,２:停用</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetScreenTeam")]
        public IHttpActionResult GetScreenTeam(string name, string hospital, string ownerid, int statecode=0)
        {
            try
            {
                log.Info(string.Format("GetScreenTeam name:'{0}',hospital:'{1}',ownerid:'{2}',statecode:{3}", name, hospital, ownerid, statecode));
                IEnumerable<ScreenTeamView> screenTeams = screenTeamBll.QueryScreenTeam(name, hospital, ownerid, statecode);
                return Ok<IEnumerable<ScreenTeamView>>(screenTeams);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }

        /// <summary>
        /// 根据小组ID获取小组成员
        /// </summary>
        /// <param name="teamid"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetScreenTeamMemberByTeamid")]
        public IHttpActionResult GetScreenTeamMemberByTeamid(string teamid)
        {
            return Ok<dynamic>(screenTeamBll.GetScreenTeamMemberByTeamid(teamid));
        }
    }
}
